﻿var userlist = {
    formDefine: {
        labelWidth:30,
        fields: [
                    { display: lang.Organization_structure, name: "ORGANIZATION_ID", type: "selecttree", labelWidth: 110, inputWidth: 170 },
                   // { display: "工号", name: "USER_CODE", type: "text" },
                    {display: lang.Account, name: "USER_ACCOUNT", type: "text", labelWidth: 40 },
                    { display: lang.name, name: "USER_NAME", type: "text" },
                    { display: lang.Query, name: "search", type: "button", click: function () { userlist.search(); } },
                    { display: lang.add, name: "add", type: "button", click: function () { openPage('useredit', '', '', lang.add_user); } }
                ],
        fieldsInit: function () { 
            loadOrgData();
        }
    },
    gridDefine: {
        columns: [

           { display: lang.user_name, name: 'USER_NAME', minWidth: 90 }, //{ display: '用户姓名', name: 'USER_NAME', minWidth: 90 },
            {display: lang.Account, name: 'USER_ACCOUNT', minWidth: 80 },
//            { display: '性别', name: 'USER_SEX', minWidth: 80 ,render:LinkSex},
//            { display: '职称', name: 'TITLE', minWidth: 80 },
//            { display: '邮箱', name: 'EMAIL', minWidth: 140 },
           // { display: '状态', name: 'DELETEMARK', minWidth: 120 },
        //            {display: '手机', name: 'TELPHONE', minWidth: 140 },

            {display: lang.Department_F, name: 'ORGANIZATION_NAME', minWidth: 200 },
            { display: lang.group, name: 'ROLES_NAMELIST', minWidth: 200 },
            { display: lang.zone_state, name: 'SPACE_STATUS', minWidth: 62, align: 'center', render: LinkSpaceStatus },
            { display: lang.size_of_the_zone_, name: 'SPACE_SIZE', minWidth: 90, align: 'center' },
            { display: lang.used_size_of_zone, name: 'SPACE_CURE_USE', minWidth: 90, align: 'center' },
            { display: lang.score, name: 'INTEGRAL', minWidth: 70, align: 'center' },
            { display: lang.operation, name: 'USER_ID', minWidth: 205, render: LinkOP, align: 'center' }
        ],
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryUserInfoPageList"
    },
    init: function () {
        jsPageRender(this,true);
    },
    search: function () {
        jsPageSearch(this);
    }
};

function LinkOP(value, row, index) {
    var _str = 'userid=' + row.USER_ID,spacestatus='';

    if (row.SPACE_STATUS == "1") {
        spacestatus = '<a href="javascript:void(0)" onclick="handlerspaceStatus(\'' + row.USER_ID + '\',0);">'+lang.lock+'</a>';
    } else {
        spacestatus = '<a href="javascript:void(0)" onclick="handlerspaceStatus(\'' + row.USER_ID + '\',1);"><span style="color:red">'+lang.unlock+'</span></a>';
    }
    spacestatus += '<span style="padding-left:15px;"><a href="javascript:void(0)" onclick="resetpassword(\'' + row.USER_ID + '\',0);">'+lang.reset_password+'</a></span>';
    return '<span style="padding-right:15px;"><a href="javascript:void(0)" onclick="openPage(\'useredit\',\'' + _str + '\');">' + lang.edit + '</a></span><span style="padding-right:15px;"><a href="javascript:void(0)" onclick="deleteuser(\'' + row.USER_ID + '\',\'' + row.USER_NAME + '\');">' + lang.delete_ + '</a></span>' + spacestatus;
}

function LinkSex(value, row, index) {
    if (row.USER_SEX == '1') { return lang.male; }
    else {
        return lang.female;
    }
}

function LinkSpaceStatus(value, row, index) {
    if (row.SPACE_STATUS == "1") { return lang.open; }
    else {
        return lang.close;
    }
}


function handlerspaceStatus(userid, status) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "UpdateSpaceStatus",
        model: function () { return $.toJSON({ 'SPACE_STATUS': status, 'USER_ID': userid }); },
        success: function (data) {
            if (data) {
                userlist.search();
            }
        }
    });

}

//#region 重置密码
function resetpassword(userid) {
    $.confirm(lang.reset_password + "?", lang.reset_password, function (isbool) {
        if (isbool) {
            $.ajaxHandler({
                url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
                urlType: "ResetPassword",
                model: function () { return $.toJSON({ 'userId': userid, 'password': hex_md5(Sys_Config.ResetPassword) }); },
                success: function (data) {
                    if (data) {
                        $.alert(lang.reset_password_to+":" + Sys_Config.ResetPassword);
                    }
                }
            });
        }
    });
    
}
//#endregion

//#region 加载营业部数据
function loadOrgData(_currentval) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryOrganizeList",
        success: function (data) {
            if (data && data.length > 0) {
                var _dataarr = [];
                var rootobj = {};
                rootobj.id = '';
                rootobj.text = lang.all;
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ORGANIZATION_ID;

                        obj.text = data[i].ORGANIZATION_NAME;
                        getsubOrg(data, data[i].ORGANIZATION_ID, obj);
                        _data.push(obj);
                    }
                }
                rootobj.children = _data;
                _dataarr.push(rootobj);
                $('#formidORGANIZATION_ID').combotree({ multiple: false });
                $('#formidORGANIZATION_ID').combotree('loadData', _dataarr);
//                if (_currentval&&_currentval != '') {
//                    $('#formidORGANIZATION_ID').combotree('setValue', _currentval);
//                }

            }

        }
    });

    function getsubOrg(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ORGANIZATION_ID;
                    obj.text = _d[i].ORGANIZATION_NAME;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubOrg(_d, _sdata[j].ORGANIZATION_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

//#region 删除
function deleteuser(userid, username) {
    $.extend($.messager.defaults, {
        ok: lang.ok,
        cancel: lang.cancel
    });
    $.confirm(lang.Sure_to_delete_user + "[" + username + "]", lang.delete_user, function (b) {
        if (b) {
            $.ajaxHandler({
                url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
                urlType: "EditUserInfo",
                model: function () { return $.toJSON({ 'action': 0, 'USER_ID': userid }); },
                success: function (data) {
                    $.alert(lang.Successfully_deleted, null, function () { userlist.search(); });
                }
            });
        }
    });
}
//#endregion

